package com.ly.mpayment.rest.system.slow;

import com.ly.mpayment.core.util.*;
import com.ly.mpayment.persist.entity.Mobile;
import com.ly.mpayment.persist.entity.slow.SlowProduct;
import com.ly.mpayment.persist.service.mobile.IMobileService;
import com.ly.mpayment.persist.service.slow.SlowProductService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;
import com.ly.mpayment.rest.recharge.RechargeRest;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import java.math.BigDecimal;
import java.util.Map;

@Component
@Path("system/product")
public class SlowProductRest extends AbsRest {
	Logger logger = Logger.getLogger(SlowProductRest.class);
	@Autowired
	IUserService userService;
	
	@Autowired
	SlowProductService slowProductService;

	@Autowired
	IMobileService mobileService;

	@Autowired
	private RechargeRest rechargeRest;
	
	@SuppressWarnings("static-access")
	@POST
	@Path("addProduct")
	public String addProduct(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "price","discount","discountAmount","remark");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			SlowProduct product = (SlowProduct) json.toBean(json, SlowProduct.class);
			product.setCreateTime(DateUtils.getNowDate());
			product.setStatus(0);
			if(slowProductService.saveProduct(product)){
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt(ResultMessage.buscodeError);
		}
		return result;
	}
	
	@SuppressWarnings("static-access")
	@POST
	@Path("updateProduct")
	public String updateProduct(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "price","discount","discountAmount","remark","proId");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			SlowProduct product = slowProductService.findById(json.getString("proId"));
			product.setPrice(json.getString("price"));
			product.setDiscount(new BigDecimal(json.getString("discount")));
			product.setDiscountAmount(new BigDecimal(json.getString("discountAmount")));
			product.setRemark(json.getString("remark"));
			product.setOperator(json.getString("operator"));
			if(slowProductService.updateState(product)){
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt(ResultMessage.buscodeError);
		}
		return result;
	}
	
	@POST
	@Path("updateState")
	public String updateState(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "status","proId");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			SlowProduct product = slowProductService.findById(json.getString("proId"));
			product.setStatus(json.getInt("status"));
			if(slowProductService.updateState(product)){
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt(ResultMessage.buscodeError);
		}
		return result;
	}
	

	@Path("findPageProduct")
	@POST
	public String findPageProduct(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService,"start","pageSize","price");
		if(result == null){
			Map<String, Object> parameters = MapUtil.getMap();
			JSONObject json = JsonUtils.converToJson(data);
			if(!StringUtils.isBlank(json.getString("price"))){
				parameters.put(":price", json.getString("price"));
			}
			if(!StringUtils.isBlank(json.getString("price"))){
				parameters.put(":price", json.getString("price"));
			}
			return ResultMessage.getSucceed( JsonUtils.excludes(this.slowProductService.findPageProduct(parameters, json.getInt("start"), json.getInt("pageSize")),
					new String[]{}));
		}
		return result;
	}

	/**
	 * 查询所有可用产品
	 * @param request
	 * @return
	 */
	@Path("findAll")
	@GET
	public String findAll(@Context HttpServletRequest request){
		return ResultMessage.getSucceed(JsonUtils.convertListToJson(this.slowProductService.findAll(""), new String[]{}));
	}

	/**
	 * 按手机号查询手机归属，+可用产品
	 * @param request
	 * @return
	 */
	@Path("findByPhone")
	@GET
	public String findByPhone(@Context HttpServletRequest request, @QueryParam("phone")String phone){
		if(StringUtils.isBlank(phone) || phone.trim().length() != 11){
			return ResultMessage.getFailt("手机号码错误");
		}
		Mobile mobile = rechargeRest.checkMobile(phone);
		if(mobile == null){
			return ResultMessage.getFailt("不支持的号码");
		}
		com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
		jsonObject.put("province",mobile.getProvince());
		jsonObject.put("city",mobile.getCity());
		jsonObject.put("operators",mobile.getServiceProvider());
		jsonObject.put("list",this.slowProductService.findAll(mobile.getServiceProvider()));
		return ResultMessage.getSucceed(jsonObject.toString());
	}

}
